<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="user" /><span>老六部门管理</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 树状结构 -->
        <el-tree default-expand-all :data="list">
          <template #default="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width:100%">
              <el-col :span="20">
                <svg-icon icon-class="tree" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdd(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length ===0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <!-- 弹层 -->
      <el-dialog
        :title="isEdit?'修改部门':'添加部门'"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialog"
      >
        <!-- v-if="showDialog" -->
        <addOrUpdata
          v-if="showDialog"
          :id="curId"
          ref="updataRef"
          :is-edit="isEdit"
          :origin-list="originList"
          @success="hSuccess"
          @close="hClose"
        />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getDepartmentList, delDepartments } from '@/api/departments'
import { transDaraToTreeData } from '@/utils'
import addOrUpdata from '@/views/departments/deptDialog.vue'
export default {
  components: {
    addOrUpdata
  },
  data() {
    return {
      list: [],
      showDialog: false,
      curId: '',
      isEdit: false, // 是否处于编辑状态
      originList: []
    }
  },
  created() {
    this.loadDepartmentList()
  },
  methods: {
    // 关闭弹窗
    hClose() {
      this.showDialog = false
      // this.$refs.updataRef.resetfrom()
    },
    hDel(id) {
      this.$confirm('此操作将永久删除部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 做删除
        const res = await delDepartments(id)
        if (res.code !== 10000) return this.$message.error(res.message)
        this.loadDepartmentList()
        this.$message.success(res.message)
      }).catch(err => err)
    },
    // 添加后关闭页面
    hSuccess() {
      this.loadDepartmentList()
      this.showDialog = false
    },
    // gx
    hEdd(id) {
      this.curId = id
      this.isEdit = true
      this.showDialog = true
      // this.$nextTick(() => {
      //   this.$refs.updataRef.loadDepartmentDetail()
      // })
    },
    // 添加
    hAdd(id) {
      this.curId = id
      this.isEdit = false
      this.showDialog = true
    },
    // 请求列表
    async loadDepartmentList() {
      const res = await getDepartmentList()
      // console.log('res.data.depts', res.data.depts)
      this.originList = res.data.depts.map(item => {
        return { id: item.id, pid: item.pid, code: item.code, name: item.name }
      })
      res.data.depts.shift()
      this.list = transDaraToTreeData(res.data.depts)
    }
  }
}
</script>
